package com.example.vv.model.main.base.presenter;

import android.app.Activity;
import android.widget.Toast;

import com.example.vv.fragmentWork.entity.LoginEntity;
import com.example.vv.fragmentWork.mvp.MvpPresenter;
import com.example.vv.fragmentWork.mvp.ViewISNullException;
import com.example.vv.fragmentWork.utlis.ShareUtils;
import com.example.vv.fragmentWork.utlis.UserInfo;
import com.example.vv.model.main.base.contract.MainContract;
import com.example.vv.model.main.base.model.MainModel;
import com.example.vv.req.REQ_LOGIN;
import com.example.vv.res.net.BaseResponse;
import com.google.gson.Gson;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


/**
 * 网络请求
 */
public class MainPresenter extends MvpPresenter<MainContract.View,MainContract.Model> implements MainContract.Presenter {
    @Override
    public void attach(MainContract.View view) {
        super.attach(view);
        model = new MainModel();
    }
    public void initData(){
        try {
            UserInfo userInfo = ShareUtils.getUserInfo((Activity) view().getMContext());
            if (userInfo!=null) {
                model.login(new REQ_LOGIN(userInfo.getName(),userInfo.getPassword()))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .map(new Function<BaseResponse, LoginEntity>() {
                            @Override
                            public LoginEntity apply(BaseResponse baseResponse) throws Exception {
                                return new Gson().fromJson(baseResponse.getData(),LoginEntity.class);
                            }
                        }).subscribe(new Observer<LoginEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LoginEntity loginEntity) {
                        try {
                            ShareUtils.addUserInfo((Activity)view().getMContext(),loginEntity);

                        } catch (ViewISNullException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        try {
                            Toast.makeText(view().getMContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                        } catch (ViewISNullException ex) {
                            ex.printStackTrace();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
            }


        } catch (ViewISNullException e) {
            e.printStackTrace();
        }
    }
}
